/* ********************************
 * 
 * author:  sunsc
 * date:    06/25/2013
 * update:  06/25/2013
 * file: 		databaseOper.c 
 * brief: Copyright (c) 2013 Sand
 * 
 ********************************/

#include "databaseOper.h"

redisContext* dBConnect(unsigned int idBId)
{
      redisReply *reply;
      int       nRet = OK;
      redisContext *con;
      /* set timeout 1.5 seconds */
      struct timeval timeout = { 1, 500000 }; 
      con = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);
      if (con->err){
          printf("Connection error: %s\n", con->errstr);
          nRet = NOT_OK;
      }
    
      /* test server */
      reply = redisCommand(con,"PING");
      if ( 0 == memcmp(reply->str,"PONG",sizeof("PONG"))){
        nRet = OK;
      }else{
        nRet = NOT_OK;
      }
      freeReplyObject(reply);
      
      /* select database 0 */
      reply = redisCommand(con,"select %d",idBId);
      if (REDIS_REPLY_ERROR == reply->type){
        nRet = NOT_OK;
      }
      freeReplyObject(reply);
      
      return con;
}








